package com.zkthink.ceres.promotion.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.ceres.common.constant.BizConstant;
import com.zkthink.ceres.common.enums.YesOrNoEnum;
import com.zkthink.ceres.promotion.dto.SalesAuditDTO;
import com.zkthink.ceres.promotion.dto.SalesPageDTO;
import com.zkthink.ceres.promotion.dto.SalesUpdateDTO;
import com.zkthink.ceres.promotion.entity.Sales;
import com.zkthink.ceres.promotion.entity.SalesPromConfig;
import com.zkthink.ceres.promotion.entity.SalesRelationConfig;
import com.zkthink.ceres.promotion.service.SalesPromConfigService;
import com.zkthink.ceres.promotion.service.SalesRelationConfigService;
import com.zkthink.ceres.promotion.service.SalesService;
import com.zkthink.ceres.promotion.vo.admin.SalesAdminPageVO;
import com.zkthink.ceres.promotion.vo.admin.SalesPerformanceAdminVO;
import com.zkthink.ceres.promotion.vo.admin.SalesPerformanceDetailAdminVO;
import com.zkthink.ceres.tenant.dao.TenantMapper;
import com.zkthink.ceres.tenant.entity.Tenant;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.security.annotation.PreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 前端控制器 分销员
 * </p>
 *
 * @author kellen
 * @date 2020-08-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sales")
@Api(value = "分销员", tags = "分销员")
@PreAuth(replace = "sales:")
@AllArgsConstructor
public class SalesController {

  private SalesService salesService;

  private SalesPromConfigService salesPromConfigService;

  private TenantMapper tenantMapper;

  private SalesRelationConfigService salesRelationConfigService;

  /**
   * 分页查询分销员
   */
  @ApiOperation("分页查询分销员")
  @PostMapping("/page")
  public R<IPage<SalesAdminPageVO>> page(@RequestBody PageParams<SalesPageDTO> params) {
    params.setSort("sort");
    params.setOrder("ascending");
    return R.success(salesService.page(params));
  }


  /**
   * 分销员审核
   */
  @ApiOperation("分销员审核")
  @PostMapping("/audit")
  public R audit(@RequestBody @Validated SalesAuditDTO auditDTO) {
    //查询分销员状态
    Sales sales = salesService.getById(auditDTO.getSalesId());
    if (null == sales) {
      return R.validFail("分销员不存在");
    }
    if (0 == sales.getAuditStatus()) {
      return R.validFail("该分销员已审核");
    }
    sales.setAuditStatus(auditDTO.getAuditStatus());
    salesService.updateById(sales);
    return R.success();
  }


  /**
   * 分销员清退
   */
  @ApiOperation("分销员清退")
  @PostMapping("/quit")
  public R quit(@RequestBody @Validated SalesAuditDTO auditDTO) {
    return salesService.quit(auditDTO.getSalesId());
  }


  /**
   * 更新
   */
  @ApiOperation("更新")
  @PostMapping("/update")
  public R update(@RequestBody @Validated SalesUpdateDTO updateDTO) {
    return salesService.updateSales(updateDTO);
  }


  /**
   * 查询分销员业绩
   */
  @ApiOperation("查询分销员业绩")
  @PostMapping("/querySalesPerformance")
  public R<IPage<SalesPerformanceAdminVO>> querySalesPerformance(
      @RequestBody PageParams<SalesPageDTO> params) {
    params.setSort(null);
    return R.success(salesService.querySalesPerformance(params));
  }


  /**
   * 查询分销员业绩详情
   */
  @ApiOperation("查询分销员业绩详情")
  @GetMapping("/querySalesPerformanceDetail")
  public R<SalesPerformanceDetailAdminVO> querySalesPerformanceDetail(
      @RequestParam("salesId") String salesId) {
    return R.success(salesService.querySalesPerformanceDetail(Long.valueOf(salesId)));
  }

  /**
   * 修复之前的商家的分销配置
   */
  @ApiOperation("修复之前的商家的分销配置")
  @PostMapping("/fixSalesConfig")
  public R<Boolean> fixSalesConfig() {
    List<Tenant> tenantList = tenantMapper.selectList(Wraps.<Tenant>lbQ().ne(Tenant::getCode, BizConstant.SUPER_TENANT));
    if(tenantList != null && tenantList.size() > 0){
      for(Tenant tenant:tenantList){
        BaseContextHandler.setTenant(tenant.getCode());
        SalesPromConfig salesPromConfig1 = salesPromConfigService.getOne(Wraps.<SalesPromConfig>lbQ().eq(SalesPromConfig::getType,1).eq(SalesPromConfig::getIsDelete, YesOrNoEnum.NO.getCode()));
        SalesPromConfig salesPromConfig2 = salesPromConfigService.getOne(Wraps.<SalesPromConfig>lbQ().eq(SalesPromConfig::getType,2).eq(SalesPromConfig::getIsDelete, YesOrNoEnum.NO.getCode()));
        SalesRelationConfig salesRelationConfig = salesRelationConfigService.getOne(Wraps.<SalesRelationConfig>lbQ().eq(SalesRelationConfig::getIsDelete, YesOrNoEnum.NO.getCode()));
        if(salesPromConfig1 == null){
          salesPromConfigService.save(SalesPromConfig.builder().type(1).isShowShopLogo(1).isShowNickName(1).build());
        }
        if(salesPromConfig2 == null){
          salesPromConfigService.save(SalesPromConfig.builder().type(2).isShowShopLogo(1).isShowNickName(1).build());
        }
        if(salesRelationConfig == null){
          salesRelationConfigService.save(SalesRelationConfig.builder().isForeverValid(YesOrNoEnum.YES.getCode()).protectTime(30).rebindCustomerFlag(2).salesBindFalg(1).selfBuyReturnCommisionFlag(2).build());
        }
      }
    }
    return R.success(true);
  }

}
